package com.neusoft.yiyang.user.service.impl;

import com.neusoft.yiyang.common.entity.user.Role;
import com.neusoft.yiyang.user.mapper.RoleMapper;
import com.neusoft.yiyang.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> getAllRoles() {
        // 查询所有未删除的角色
        return roleMapper.selectList(null);
    }

    @Override
    public Role getRoleById(Integer roleId) {
        // 根据角色ID查询角色
        Role role = roleMapper.selectById(roleId);
        if (role == null) {
            throw new RuntimeException("Role not found");
        }
        return role;
    }

    @Override
    public void createRole(Role role) {
        // 插入新角色
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        // 更新角色
        roleMapper.updateById(role);
    }

    @Override
    public void deleteRole(Integer roleId) {
        // 逻辑删除角色：将 is_deleted 设置为 1
        Role role = new Role();
        role.setRoleId(roleId);
        role.setStatus(0);  // 假设 status 0 表示禁用，等同于删除
        roleMapper.updateById(role);
    }
}
